// 思路：将要改变this指向的方法挂到目标this上执行并返回
/**
 * defCall(this,...arg)
 * @param target
 * @returns {*}
 */
Function.prototype.defCall = function(target){
    if(typeof this !== 'function'){
       throw new TypeError('被绑定的对象需要是函数')
    }
    target = target || window
    target.fn = this
    //[...arguments][0]是this,后面的才是参数
    let arg = [...arguments].slice(1)
    let result = target.fn(...arg)
    delete target.fn
    return result
}


//测试
function log(){
    console.log.defCall(this,...arguments)
}
log([1,2,3].slice(1),2)


//测试
const obj1 = {
    context:'obj1 context'
}
const obj2 = {
    context:2,
    fn:function(){
        console.log(this.context)
    }
}

obj2.fn.defCall(obj1) // obj1 context